package com.qiantu.api.db.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.qiantu.api.db.greendao.ControllerBeanDao;
import com.qiantu.api.db.greendao.DaoMaster;
import com.qiantu.api.db.greendao.DaoSession;
import com.qiantu.api.db.greendao.DeviceBeanDao;
import com.qiantu.api.db.greendao.DeviceBindGroupBeanDao;
import com.qiantu.api.db.greendao.DeviceCustomKeyBeanDao;
import com.qiantu.api.db.greendao.FloorBeanDao;
import com.qiantu.api.db.greendao.HouseBeanDao;
import com.qiantu.api.db.greendao.MusicDaoBeanDao;
import com.qiantu.api.db.greendao.RoomBeanDao;
import com.qiantu.api.db.greendao.SceneBeanDao;
import com.qiantu.api.db.greendao.SourceHomepageBeanDao;
import com.qiantu.api.db.greendao.UniversalDeviceDataDao;
import com.qiantu.api.entity.UniversalDeviceData;

import org.greenrobot.greendao.identityscope.IdentityScopeType;

public class LocalDataBase {
    private static final String DBNAME = "linglu.db";
    private volatile static LocalDataBase instance;
    private DaoMaster daoMaster;
    private DaoSession daoSession;

    public static LocalDataBase getInstance() {

        if (instance == null) {
            synchronized (LocalDataBase.class) {
                if(instance == null)
                    instance = new LocalDataBase();
            }
        }

        return instance;
    }

    private LocalDataBase() {
    }

    /**
     * 获取DaoMaster
     *
     * @param context
     * @return
     */
    public DaoMaster getDaoMaster(Context context){
        MySQLiteOpenHelper devOpenHelper = new MySQLiteOpenHelper(context, DBNAME, null);
        daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        return daoMaster;
    }

    /**
     * 获取DaoSession对象
     *
     * @param context
     * @return
     */
    public DaoSession getDaoSession(Context context) {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster(context);
            }
                daoSession = daoMaster.newSession(IdentityScopeType.None);//不允许缓存
        }

        return daoSession;
    }

    /**
     * 适用于未加密的数据库
     */
    private static class MySQLiteOpenHelper extends DaoMaster.OpenHelper {

        public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
            super(context, name, factory);
        }

        private static final String UPGRADE="upgrade";

        //新加表之后，在MigrationHelper.migrate添加dao.class，并在build.gradle里数据库版本号+1
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            MigrationHelper.migrate(db,
                    DeviceBeanDao.class, FloorBeanDao.class, HouseBeanDao.class, RoomBeanDao.class, SceneBeanDao.class, ControllerBeanDao.class, MusicDaoBeanDao.class, DeviceBindGroupBeanDao.class, SourceHomepageBeanDao.class, DeviceCustomKeyBeanDao.class, UniversalDeviceDataDao.class);
            Log.e(UPGRADE,"upgrade run success");
        }
    }

    /**
     * 适用于加密的数据库
     */
//    private static class MyEncryptedSQLiteOpenHelper extends DaoMaster.OpenHelper {
//
//        private final Context context;
//        private final String name;
//        private final int version = DaoMaster.SCHEMA_VERSION;
//
//        private boolean loadSQLCipherNativeLibs = true;
//
//        public MyEncryptedSQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
//            super(context, name, factory);
//            this.context=context;
//            this.name=name;
//        }
//
//        private static final String UPGRADE="upgrade";
//
//        @Override
//        public void onUpgrade(Database db, int oldVersion, int newVersion) {
//
////            EncryptedMigrationHelper.migrate((EncryptedDatabase) db, TbAutomaticDeviceGenerateConfigDao.class);
//            Log.e(UPGRADE,"upgrade run success");
//        }
//
//        @Override
//        public Database getEncryptedWritableDb(String password) {
//            MyEncryptedHelper encryptedHelper = new MyEncryptedHelper(context,name,version,loadSQLCipherNativeLibs);
//            return encryptedHelper.wrap(encryptedHelper.getReadableDatabase(password));
//        }
//
//        private class MyEncryptedHelper extends net.sqlcipher.database.SQLiteOpenHelper {
//            public MyEncryptedHelper(Context context, String name, int version, boolean loadLibs) {
//                super(context, name, null, version);
//                if (loadLibs) {
//                    net.sqlcipher.database.SQLiteDatabase.loadLibs(context);
//                }
//            }
//
//            @Override
//            public void onCreate(net.sqlcipher.database.SQLiteDatabase db) {
//                MyEncryptedSQLiteOpenHelper.this.onCreate(wrap(db));
//            }
//
//            @Override
//            public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, int oldVersion, int newVersion) {
//                MyEncryptedSQLiteOpenHelper.this.onUpgrade(wrap(db), oldVersion, newVersion);
//            }
//
//            @Override
//            public void onOpen(net.sqlcipher.database.SQLiteDatabase db) {
//                MyEncryptedSQLiteOpenHelper.this.onOpen(wrap(db));
//            }
//
//            protected Database wrap(net.sqlcipher.database.SQLiteDatabase sqLiteDatabase) {
//                return new EncryptedDatabase(sqLiteDatabase);
//            }
//        }
//    }
}
